package cn.moukou.sbzz.controller;

import cn.moukou.sbzz.pojo.Enterprise;
import cn.moukou.sbzz.service.AdminService;
import cn.moukou.sbzz.service.BankService;
import cn.moukou.sbzz.service.EnterpriseService;
import cn.moukou.sbzz.util.JwtUtil;
import cn.moukou.sbzz.util.Resp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/sbzz/api")
public class EnterpriseController {

    @Autowired
    EnterpriseService enterpriseService;
    @Autowired
    BankService bankService;
    @Autowired
    AdminService adminService;

    @GetMapping("/enterprise/list")
    public Resp<List<Enterprise>> getEnterpriseList() {
        List<Enterprise> enterpriseList = enterpriseService.getEnterpriseList();
        return Resp.ok(enterpriseList);
    }

    @GetMapping("/enterprise/to-auth")
    public Resp<List<Enterprise>> getEnterpriseToAuth() {
        return Resp.ok(enterpriseService.getEnterpriseToAuth());
    }

    @GetMapping("/enterprise/one")
    public Resp<Enterprise> getOneEnterprise(@RequestParam("id") String id) {
        return Resp.ok(enterpriseService.getOneEnterprise(id));
    }

    @GetMapping("/enterprise/my")
    public Resp<Enterprise> getOneEnterprise(HttpServletRequest request) throws Exception {
        String id = JwtUtil.getUid(request);
        return Resp.ok(enterpriseService.getOneEnterprise(id));
    }

    @PostMapping("/enterprise/auth")
    public Resp<String> authEnterprise(@RequestParam("id") String id){
        if(enterpriseService.authEnterprise(id) > 0){
            return Resp.ok("认证成功");
        } else {
            return Resp.fail("认证失败");
        }
    }

    @PostMapping("/enterprise/submit")
    public Resp<String> submitEnterprise(@RequestParam("id") String id, @RequestParam("name") String name,
                                 @RequestParam("detail") String detail, @RequestParam("link") String link){
        if(enterpriseService.submitEnterprise(id, name, detail, link) > 0){
            return Resp.ok("提交成功");
        } else {
            return Resp.fail("提交失败");
        }
    }

    @PostMapping("/enterprise/access")
    public Resp<String> accessEnterprise(@RequestParam("id") String id, @RequestParam("form") String form,
                                   @RequestParam("business") String business, @RequestParam("pool") String pool){
        if(enterpriseService.accessEnterprise(id, form, business, pool) > 0){
            return Resp.ok("认证成功");
        } else {
            return Resp.fail("认证失败");
        }
    }

}
